package com.cloud.lawcase.api.web;

import javax.servlet.http.HttpServletRequest;

import com.cloud.lawcase.api.bean.LawItemSearchCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.lawcase.api.bean.LawSearchCriteria;
import com.cloud.lawcase.api.service.OpenApiService;
import com.cloud.lawcase.common.openapi.OpenApiCauseStackSearchCriteria;
import com.cloud.lawcase.common.openapi.OpenApiLawCauseSearchCriteria;
import com.cloud.lawcase.common.openapi.OpenApiPageInfo;
import com.cloud.lawcase.common.result.Quokka;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api("执法依据平台对公接口")
@RestController
@RequestMapping("lawcase/open/api")
public class OpenApiController {

    @Autowired
    private OpenApiService openApiService;

    @ApiOperation(value = "刷新token操作", notes = "用于申请token，每个token每天默认可调用200次，每次获取的token默认时效为15分钟", consumes = "application/x-www-form-urlencoded")
    @ApiImplicitParam(name = "appid", value = "每个调用方向执法依据平台申请的唯一标识符", required = true, paramType = "query")
    @GetMapping("refreshtoken")
    public Quokka refreshToken(HttpServletRequest request, @RequestParam(required = true) String appid) {
        Quokka quokka = new Quokka();
        try {
            String token = openApiService.refreshToken(appid);
            quokka.setOk(true);
            quokka.setData(token);
        } catch (Exception e) {
            quokka.setOk(false);
            quokka.setData(e.getMessage());
        }
        return quokka;
    }

    @ApiOperation(value = "查询法规接口", notes = "用于查询执法依据平台法律法规信息", consumes = "application/x-www-form-urlencoded")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "refreshtoken中获取的token（注，有效期为15分钟）", required = true, paramType = "header"),
            @ApiImplicitParam(name = "title", value = "法规标题中的关键字", required = false, paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = false, defaultValue = "1", paramType = "query"),
            @ApiImplicitParam(name = "funcUnitType", value = "职能部门（1.自然资源；2.住房城乡建设......）", required = false, paramType = "query"),
            @ApiImplicitParam(name = "funcUnitLevel", value = "职能层级(1.国务院主管部门;2.省主管部门......)", required = false, paramType = "query"),
            @ApiImplicitParam(name = "causeID", value = "案由ID", required = false, paramType = "query")
    })
    @GetMapping("law")
    public Quokka listLaw(HttpServletRequest request, LawSearchCriteria criteria) {
        String appId = (String) request.getAttribute("appId");
        criteria.setAppId(appId);
        return new Quokka(true, new OpenApiPageInfo(openApiService.listLaw(criteria)));
    }

    @ApiOperation(value = "查询法规内容接口", notes = "用于查询执法依据平台法律法规信息", consumes = "application/x-www-form-urlencoded")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "refreshtoken中获取的token（注，有效期为15分钟）", required = true, paramType = "header"),
            @ApiImplicitParam(name = "lawID", value = "法规ID", required = false, paramType = "query"),
            @ApiImplicitParam(name = "articleID", value = "条", required = false, paramType = "query"),
            @ApiImplicitParam(name = "paragraphID", value = "款", required = false, paramType = "query"),
            @ApiImplicitParam(name = "subparagraphID", value = "项", required = false, paramType = "query"),
            @ApiImplicitParam(name = "itemID", value = "目", required = false, paramType = "query"),
    })
    @GetMapping("lawItem")
    public Quokka listLawItem(HttpServletRequest request, LawItemSearchCriteria criteria) {
        String appId = (String) request.getAttribute("appId");
        criteria.setAppId(appId);
        return new Quokka(true, openApiService.listLawItem(criteria));
    }

    @ApiOperation(value = "查询事由接口", notes = "用于查询执法依据平台事由信息", consumes = "application/x-www-form-urlencoded")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "refreshtoken中获取的token（注，有效期为15分钟）", required = true, paramType = "header"),
            @ApiImplicitParam(name = "keyWords", value = "事由中的关键字", required = false, paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = false, defaultValue = "1", paramType = "query"),
            @ApiImplicitParam(name = "funcUnitType", value = "职能部门（1.自然资源；2.住房城乡建设......）", required = false, paramType = "query"),
            @ApiImplicitParam(name = "funcUnitLevel", value = "职能层级(1.国务院主管部门;2.省主管部门......)", required = false, paramType = "query"),
            @ApiImplicitParam(name = "lawID", value = "法规编码", required = false, paramType = "query")
    })
    @GetMapping("cause")
    public Quokka listCause(HttpServletRequest request, OpenApiLawCauseSearchCriteria criteria) {
        String appId = (String) request.getAttribute("appId");
        criteria.setAppId(appId);
        return new Quokka(true, new OpenApiPageInfo(openApiService.listCause(criteria)));
    }

    @ApiOperation(value = "查询事由裁量明细接口", notes = "用于查询执法依据平台事由裁量明细信息", consumes = "application/x-www-form-urlencoded")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "refreshtoken中获取的token（注，有效期为15分钟）", required = true, paramType = "header"),
            @ApiImplicitParam(name = "causeCode", value = "事由编码（查询事由接口中获取）", required = true, paramType = "query")
    })
    @GetMapping("causestack")
    public Quokka listCauseStack(HttpServletRequest request, OpenApiCauseStackSearchCriteria criteria) {
        String appId = (String) request.getAttribute("appId");
        criteria.setAppId(appId);
        return new Quokka(true, openApiService.listCauseStack(criteria));
    }

    @ApiOperation(value = "查询职能部门接口", notes = "用于查询执法单位所属的职能部门信息", consumes = "application/x-www-form-urlencoded")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "refreshtoken中获取的token（注，有效期为15分钟）", required = true, paramType = "header")
    })
    @GetMapping("unitFunction")
    public Quokka listUnitFunction(HttpServletRequest request) {
        return new Quokka(true, openApiService.listUnitFunction());
    }

    @ApiOperation(value = "查询职能部门级别接口", notes = "用于查询执法单位所属的职能部门级别信息", consumes = "application/x-www-form-urlencoded")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "refreshtoken中获取的token（注，有效期为15分钟）", required = true, paramType = "header")
    })
    @GetMapping("unitFunctionLevel")
    public Quokka listUnitFunctionLevel(HttpServletRequest request) {
        return new Quokka(true, openApiService.listUnitFunctionLevel());
    }

    @ApiOperation(value = "查询法规分组", notes = "用于查询法规分组信息", consumes = "application/x-www-form-urlencoded")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "refreshtoken中获取的token（注，有效期为15分钟）", required = true, paramType = "header")
    })
    @GetMapping("getLawGroup")
    public Quokka getLawGroup(HttpServletRequest request) {
        return new Quokka(true, openApiService.getLawGroup());
    }

}
